// const baseUrl = "https://api.arapp.net"
// #ifdef MP-WEIXIN
// const baseUrl = 'http://192.168.31.83:20100' // development
// const baseUrl = 'https://api.arapp.net' // production
const globalHeader = {
	header: {'Cookie': uni.getStorageSync('cookie')}
}
// #endif
import login from './login.js'
let baseUrl
if (process.env.NODE_ENV === "development") {
	// baseUrl = '/api'
	// baseUrl = 'http://192.168.31.83:20100'
	baseUrl = 'https://api.arapp.net'
} else {
	baseUrl = 'https://api.arapp.net'
}
// const baseUrl = '/api' // development
// const baseUrl = 'https://api.arapp.net' // production
const globalHeader = {
	header: {}
}
const myRequest = (options) => {
	return new Promise((resolve, reject) => {
		if (options.loading) {
			uni.showLoading({
				title: '加载中...',
				mask: true
			})
		}
		uni.request({
			url: baseUrl + options.url,
			method: options.method || 'GET', // 不传默认'GET'请求
			header: {'authorization': uni.getStorageSync('token') ? uni.getStorageSync('token') : ''},
			// header: {},
			data: options.data || {},
			success: (res) => {
				// console.log(res)
				// if (res.data.code === 3) {
				// 	uni.showToast({title: '未登入',icon: 'none'})
				// 	// 判断是否是第一次开启App
				// 	const value = uni.getStorageSync('launchFlag');
				// 	if (value) {
				// 		// launchFlag=true直接跳转到首页
				// 		console.log(value)
				// 		login().then(ret=> {
				// 			console.log(ret)
				// 		})
				// 	} else {
				// 		console.log(value)
				// 		// uni.navigateTo({
				// 		// 	url: '/pages/index/index'
				// 		// })
				// 	}
					
				// 	// let url = encodeURIComponent(window.location.href)
				// 	// window.location.href = 'https://mobile.laiba.shop/activity/visitor/auth?url=' + url
				// 	return
				// }
				if (res.data.code === 3) {
					uni.removeStorageSync('token')
					let pages = getCurrentPages()
					let page = pages[pages.length - 1]
					console.log(page.route)
					if (page.route != 'pages/user/user') {
						uni.navigateTo({url:'/pages/login/login'})
					}
					resolve(res)
					// console.log('设置成功')
				}
				if (res.data.code !== 0) {
					return uni.showToast({
						title: res.data.message || '服务器内部错误',
						icon: 'none'
					})
				}
				resolve(res)
			},
			fail: (err) => {
				// console.log(err)
				uni.showToast({title: '网络错误',icon: 'none'})
				reject(err)
			},
			complete: () => {
				// if (options.loading) {
				// 	uni.hideLoading()
				// }
				uni.hideLoading()
				uni.stopPullDownRefresh()
			}
		})
	})
}

// const showError = (error) => {
	
// }
export {
	myRequest
}